﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using AspNetCs.Model;
using System.Data;
using AspNetCs.Config;
using AspNetCs.Common;

namespace AspNetCs.SQLServerDAL
{
    public class AddPoint_DAL : IAddPoint
    {
        #region IAddPoint 成员

        public int Add(AddPoint_Model model)
        {
            int result = -1;

            #region 参数

            db.ParameterArry[] Par = new db.ParameterArry[8];
            Par[0].dbType = SqlDbType.VarChar;
            Par[0].objValue = model.CardNum;
            Par[0].parName = "@CardNum";

            Par[1].dbType = SqlDbType.VarChar;
            Par[1].objValue = model.CardPwd;
            Par[1].parName = "@CardPwd";

            Par[2].dbType = SqlDbType.VarChar;
            Par[2].objValue = model.MemberPrice;
            Par[2].parName = "@MemberPrice";

            Par[3].dbType = SqlDbType.Int;
            Par[3].objValue = model.UseType;
            Par[3].parName = "@UseType";

            Par[4].dbType = SqlDbType.Decimal;
            Par[4].objValue = model.AgentPrice;
            Par[4].parName = "@AgentPrice";

            Par[5].dbType = SqlDbType.Int;
            Par[5].objValue = model.Point;
            Par[5].parName = "@Point";

            Par[6].dbType = SqlDbType.Int;
            Par[6].objValue = model.State;
            Par[6].parName = "@State";

            Par[7].dbType = SqlDbType.Int;
            Par[7].objValue = model.UseId;
            Par[7].parName = "@Useid";



            #endregion

            db Conn = new db();
            try
            {
                string Sqlstr = "Insert into AddPoint(CardNum,CardPwd,MemberPrice,AgentPrice,Point,state,UseType,Useid) values(@CardNum,@CardPwd,@MemberPrice,@AgentPrice,@Point,@state,@UseType,@Useid)";
                result = Conn.ExcSql(Sqlstr, Par);

            }
            catch (Exception ex)
            {
                result = -1;
                new CustomException(ex);
            }
            finally
            {
                Conn.close();
            }
            return result;
        }

        public int Del(int id)
        {
            db Conn = new db();
            int result = Conn.ExcSql("delete From AddPoint where id=" + id);
            Conn.close();
            return result;
        }

        public AddPoint_Model GetModel(System.Data.DataRow dr)
        {
            AddPoint_Model model = new AddPoint_Model();
            if (dr["id"] != null) { model.Id = StringHelper.isNumber_int(dr["id"].ToString()); }
            if (dr["CardNum"] != null) { model.CardNum = dr["CardNum"].ToString(); }
            if (dr["CardPwd"] != null) { model.CardPwd = dr["CardPwd"].ToString(); }
            if (dr["MemberPrice"] != null) { model.MemberPrice = StringHelper.isNumber_decimal(dr["CardPwd"].ToString()); }
            if (dr["AgentPrice"] != null) { model.AgentPrice = StringHelper.isNumber_decimal(dr["AgentPrice"].ToString()); }
            if (dr["Point"] != null) { model.Point = StringHelper.isNumber_int(dr["Point"].ToString()); }
            if (dr["state"] != null) { model.State = StringHelper.isNumber_int(dr["state"].ToString()); }
            if (dr["UseType"] != null) { model.UseType = StringHelper.isNumber_int(dr["UseType"].ToString()); }
            if (dr["Useid"] != null) { model.UseId = StringHelper.isNumber_int(dr["Useid"].ToString()); }
            if (dr["UseTime"] != null) { model.UseTime = (dr["UseTime"].ToString()); }
            if (dr["Addtime"] != null) { model.AddTime = (dr["Addtime"].ToString()); }

            return model;
        }

        public AddPoint_Model GetModel(int id)
        {
            db Cn = new db();
            AddPoint_Model model = new AddPoint_Model();
            try
            {
                DataTable dt = Cn.GetSql_DataTable("Select Top 1 * From AddPoint where id=" + id);
                foreach (DataRow dr in dt.Rows)
                {
                    model = GetModel(dr);
                }
            }
            catch (Exception Ex)
            {
                new CustomException(Ex);
            }
            finally
            {

                Cn.close();

            }
            return model;
        }

        #endregion
    }
}
